﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Audit_Schedule
{
    public class Vara_data_Audit_Schedule_Controller : Vara_Data_IAudit_Schedule_Actions
    {
        private List<Vara_Data_IAudit_Schedule> _AuditSchedules;
        public List<Vara_Data_IAudit_Schedule> AuditSchedules
        {
            get
            {
                if (_AuditSchedules == null)
                    Refresh_Audit_Schedule_List();
                return _AuditSchedules;
            }
        }

        public Vara_Data_IAudit_Schedule ReturnAuditSchedule(int ID)
        {
            Vara_Data_IAudit_Schedule AuditScheduleToReturn;
            AuditScheduleToReturn = AuditSchedules.Find(delegate(Vara_Data_IAudit_Schedule AuditSchedule) { return AuditSchedule.ID == ID; });
            return AuditScheduleToReturn;
        }

        public void Refresh_Audit_Schedule_List()
        {
            DataTable DataTableAuditSchedules;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableAuditSchedules = NewSqlConnector.ExecuteProcedure("Return_Lookup_Audit_Schedule");
            }
            List<Vara_Data_IAudit_Schedule> ListOfAuditSchedules = new List<Vara_Data_IAudit_Schedule>();
            foreach (DataRow R in DataTableAuditSchedules.Rows)
            {
                Vara_Data_IAudit_Schedule AuditScheduleToAppend = new Vara_Data_Audit_Schedule(int.Parse(R["ID"].ToString()));
                AuditScheduleToAppend.Name = R["Name"].ToString();
                AuditScheduleToAppend.Months = int.Parse(R["Months"].ToString());
                ListOfAuditSchedules.Add(AuditScheduleToAppend);
            }
            _AuditSchedules = ListOfAuditSchedules;
        }
    }
}
